//
//  WFCheckBoxGroupView.m
//  WisdomFarm
//
//  Created by sky on 2018/2/18.
//  Copyright © 2018年 sky. All rights reserved.
//

#import "WFCheckBoxGroupView.h"
#import "WFCheckBox.h"

@interface WFCheckBoxGroupView()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSArray<WFCheckBox *> *boxes;
@property (nonatomic, strong) WFCheckBox *lastSelectedBox;


@end

@implementation WFCheckBoxGroupView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.spacing = 9;
        _scrollView = [[UIScrollView alloc] init];
        [self addSubview:_scrollView];
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsZero);
        }];
    }
    return self;
}

- (void)setTitles:(NSArray<NSString *> *)titles {
    if ([_titles isEqual:titles]) {
        return;
    }
    _titles = titles;
    [self layout];
}

- (void)layout {
    for (WFCheckBox *box in self.boxes) {
        [box removeFromSuperview];
    }
    WFCheckBox *lastBox = nil;
    @weakify(self);
    for (NSString *title in self.titles) {
        WFCheckBox *box = [[WFCheckBox alloc] init];
        box.onCheck = ^(WFCheckBox *box) {
            @strongify(self);
            if (!self.allowsMultiSelection) {
                self.lastSelectedBox.checked = NO;
                box.checked = YES;
            } else {
                box.checked = !box.checked;
            }
            self.lastSelectedBox = box;
        };
        box.textLabel.text = title;
        [self.scrollView addSubview:box];
        if (lastBox) {
            [box mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(lastBox.mas_right).offset(self.spacing);
                make.top.equalTo(self.mas_top).offset(8);
                make.bottom.equalTo(self.mas_bottom).offset(-8);
                make.width.mas_greaterThanOrEqualTo(75);
            }];
        } else {
            [box mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(self.spacing);
                make.top.equalTo(self.mas_top).offset(8);
                make.bottom.equalTo(self.mas_bottom).offset(-8);
                make.width.mas_greaterThanOrEqualTo(75);
            }];
        }
        lastBox = box;
    }
    if (lastBox) {
        [lastBox mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-self.spacing);
        }];
    }
}

- (NSIndexSet *)selectedIndexs {
    NSMutableIndexSet *set = [NSMutableIndexSet indexSet];
    for (int i = 0; i < self.boxes.count; i++) {
        WFCheckBox *box = self.boxes[i];
        if (box.checked) {
            [set addIndex:i];
        }
    }
    return [set copy];
}

- (void)selectButtonAtIndex:(NSInteger)index {
    if (index >= self.boxes.count) {
        return;
    }
    if (!self.allowsMultiSelection) {
        self.lastSelectedBox.checked = NO;
    }
    self.boxes[index].checked = YES;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
